module(...,package.seeall)

local storyboard = require( "storyboard" )
	local scene = storyboard.newScene()
	local ui = require ("ui")
	local wEasing = require "wEasing"
	local gui = require "Stage7.Stage7HomeGui".new()
	system.activate("multitouch")
function scene:createScene(event)
	local group = self.view
end
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
	--local group = self.view

	-- audio.play( sfx.themeMusic, { onComplete = function() 
                                      -- audio.dispose( sfx.themeMusic ) 
                                      -- end } )
	-----------------------------------------------------------------------------
		
	local mainHomeGroup = self.view
	local numberStar = 0
	local arrayScore = {1100,1000, 900, 800, 600}
	local arrayNumber = {}--mang cac chu so
	local arrayArgument = {}--mang 2 bien dau vao
	local arrayImageButton = {}
	local arrayButton = {}---mang cac button
	local arrayNumberBtn = {}
	local arrayTimeScore = {}
	local arrayCount = {0,0,0}
	local number1,number2--2 so dau vao
	local randomPosition --vi tri ket qua
	local result
	local keyListener = false
	local timeReady = 1
	local deltaTime = 1600
	local statusBoard = "start"
	local timeSystem = 0
	local delay = 100
	local loop = 0
	local countturn = 0
	local timeScore = 0
	local timeTotalScore = 0
	local xCenter = display.contentCenterX
	local yCenter = display.contentCenterY
	local bg
	local sum, sub,equal
	local txtCount = {}
	local iconOk, iconGood, iconPerfect
	local txtTimeScore, txtTimeReady, txtTimePlay
	local gameOver, win
	local go
	local btnPause
	a = 0
	function creatArrayNumber()
		arrayNumber[0] = display.newImageRect(mainHomeGroup, gui.zero.fn, gui.zero.w, gui.zero.h)
			arrayNumber[0].fn = gui.zero.fn
			arrayNumber[0].w = gui.zero.w
			arrayNumber[0].h = gui.zero.h
		arrayNumber[1] = display.newImageRect(mainHomeGroup, gui.one.fn, gui.one.w, gui.one.h)
			arrayNumber[1].fn = gui.one.fn
			arrayNumber[1].w = gui.one.w
			arrayNumber[1].h = gui.one.h
		arrayNumber[2]= display.newImageRect(mainHomeGroup, gui.two.fn, gui.two.w, gui.two.h)
			arrayNumber[2].fn = gui.two.fn
			arrayNumber[2].w = gui.two.w
			arrayNumber[2].h = gui.two.h
		arrayNumber[3]= display.newImageRect(mainHomeGroup, gui.three.fn, gui.three.w, gui.three.h)
			arrayNumber[3].fn = gui.three.fn
			arrayNumber[3].w = gui.three.w
			arrayNumber[3].h = gui.three.h
		arrayNumber[4]= display.newImageRect(mainHomeGroup, gui.four.fn, gui.four.w, gui.four.h)
			arrayNumber[4].fn = gui.four.fn
			arrayNumber[4].w = gui.four.w
			arrayNumber[4].h = gui.four.h
		arrayNumber[5]= display.newImageRect(mainHomeGroup, gui.five.fn, gui.five.w, gui.five.h)
			arrayNumber[5].fn = gui.five.fn
			arrayNumber[5].w = gui.five.w
			arrayNumber[5].h = gui.five.h
		arrayNumber[6]= display.newImageRect(mainHomeGroup, gui.six.fn, gui.six.w, gui.six.h)
			arrayNumber[6].fn = gui.six.fn
			arrayNumber[6].w = gui.six.w
			arrayNumber[6].h = gui.six.h
		arrayNumber[7]= display.newImageRect(mainHomeGroup, gui.seven.fn, gui.seven.w, gui.seven.h)
			arrayNumber[7].fn = gui.seven.fn
			arrayNumber[7].w = gui.seven.w
			arrayNumber[7].h = gui.seven.h
		arrayNumber[8]= display.newImageRect(mainHomeGroup, gui.eight.fn, gui.eight.w, gui.eight.h)
			arrayNumber[8].fn = gui.eight.fn
			arrayNumber[8].w = gui.eight.w
			arrayNumber[8].h = gui.eight.h
		arrayNumber[9]= display.newImageRect(mainHomeGroup, gui.nine.fn, gui.nine.w, gui.nine.h)
			arrayNumber[9].fn = gui.nine.fn
			arrayNumber[9].w = gui.nine.w
			arrayNumber[9].h = gui.nine.h
			----------------------------------------------
		arrayNumberBtn[0] = display.newImageRect(mainHomeGroup, gui.bzero.fn, gui.bzero.w, gui.bzero.h)
			arrayNumberBtn[0].fn = gui.bzero.fn
			arrayNumberBtn[0].w = gui.bzero.w
			arrayNumberBtn[0].h = gui.bzero.h
		arrayNumberBtn[1] = display.newImageRect(mainHomeGroup, gui.bone.fn, gui.bone.w, gui.bone.h)
			arrayNumberBtn[1].fn = gui.bone.fn
			arrayNumberBtn[1].w = gui.bone.w
			arrayNumberBtn[1].h = gui.bone.h
		arrayNumberBtn[2]= display.newImageRect(mainHomeGroup, gui.btwo.fn, gui.btwo.w, gui.btwo.h)
			arrayNumberBtn[2].fn = gui.btwo.fn
			arrayNumberBtn[2].w = gui.btwo.w
			arrayNumberBtn[2].h = gui.btwo.h
		arrayNumberBtn[3]= display.newImageRect(mainHomeGroup, gui.bthree.fn, gui.bthree.w, gui.bthree.h)
			arrayNumberBtn[3].fn = gui.bthree.fn
			arrayNumberBtn[3].w = gui.bthree.w
			arrayNumberBtn[3].h = gui.bthree.h
		arrayNumberBtn[4]= display.newImageRect(mainHomeGroup, gui.bfour.fn, gui.bfour.w, gui.bfour.h)
			arrayNumberBtn[4].fn = gui.bfour.fn
			arrayNumberBtn[4].w = gui.bfour.w
			arrayNumberBtn[4].h = gui.bfour.h
		arrayNumberBtn[5]= display.newImageRect(mainHomeGroup, gui.bfive.fn, gui.bfive.w, gui.bfive.h)
			arrayNumberBtn[5].fn = gui.bfive.fn
			arrayNumberBtn[5].w = gui.bfive.w
			arrayNumberBtn[5].h = gui.bfive.h
		arrayNumberBtn[6]= display.newImageRect(mainHomeGroup, gui.bsix.fn, gui.bsix.w, gui.bsix.h)
			arrayNumberBtn[6].fn = gui.bsix.fn
			arrayNumberBtn[6].w = gui.bsix.w
			arrayNumberBtn[6].h = gui.bsix.h
		arrayNumberBtn[7]= display.newImageRect(mainHomeGroup, gui.seven.fn, gui.seven.w, gui.seven.h)
			arrayNumberBtn[7].fn = gui.bseven.fn
			arrayNumberBtn[7].w = gui.bseven.w
			arrayNumberBtn[7].h = gui.bseven.h
		arrayNumberBtn[8]= display.newImageRect(mainHomeGroup, gui.beight.fn, gui.beight.w, gui.beight.h)
			arrayNumberBtn[8].fn = gui.beight.fn
			arrayNumberBtn[8].w = gui.beight.w
			arrayNumberBtn[8].h = gui.beight.h
		arrayNumberBtn[9]= display.newImageRect(mainHomeGroup, gui.bnine.fn, gui.bnine.w, gui.bnine.h)
			arrayNumberBtn[9].fn = gui.bnine.fn
			arrayNumberBtn[9].w = gui.bnine.w
			arrayNumberBtn[9].h = gui.bnine.h
		arrayNumberBtn[10]= display.newImageRect(mainHomeGroup, gui.bten.fn, gui.bten.w, gui.bten.h)
			arrayNumberBtn[10].fn = gui.bten.fn
			arrayNumberBtn[10].w = gui.bten.w
			arrayNumberBtn[10].h = gui.bten.h
		arrayNumberBtn[11]= display.newImageRect(mainHomeGroup, gui.beleven.fn, gui.beleven.w, gui.beleven.h)
			arrayNumberBtn[11].fn = gui.beleven.fn
			arrayNumberBtn[11].w = gui.beleven.w
			arrayNumberBtn[11].h = gui.beleven.h
		arrayNumberBtn[12]= display.newImageRect(mainHomeGroup, gui.btwelve.fn, gui.btwelve.w, gui.btwelve.h)
			arrayNumberBtn[12].fn = gui.btwelve.fn
			arrayNumberBtn[12].w = gui.btwelve.w
			arrayNumberBtn[12].h = gui.btwelve.h
		arrayNumberBtn[13]= display.newImageRect(mainHomeGroup, gui.bthirteen.fn, gui.bthirteen.w, gui.bthirteen.h)
			arrayNumberBtn[13].fn = gui.bthirteen.fn
			arrayNumberBtn[13].w = gui.bthirteen.w
			arrayNumberBtn[13].h = gui.bthirteen.h
		arrayNumberBtn[14]= display.newImageRect(mainHomeGroup, gui.bfourteen.fn, gui.bfourteen.w, gui.bfourteen.h)
			arrayNumberBtn[14].fn = gui.bfourteen.fn
			arrayNumberBtn[14].w = gui.bfourteen.w
			arrayNumberBtn[14].h = gui.bfourteen.h
		arrayNumberBtn[15]= display.newImageRect(mainHomeGroup, gui.bfifteen.fn, gui.bfifteen.w, gui.bfifteen.h)
			arrayNumberBtn[15].fn = gui.bfifteen.fn
			arrayNumberBtn[15].w = gui.bfifteen.w
			arrayNumberBtn[15].h = gui.bfifteen.h
		arrayNumberBtn[16]= display.newImageRect(mainHomeGroup, gui.bsixteen.fn, gui.bsixteen.w, gui.bsixteen.h)
			arrayNumberBtn[16].fn = gui.bsixteen.fn
			arrayNumberBtn[16].w = gui.bsixteen.w
			arrayNumberBtn[16].h = gui.bsixteen.h
		arrayNumberBtn[17]= display.newImageRect(mainHomeGroup, gui.bseventeen.fn, gui.bseventeen.w, gui.bseventeen.h)
			arrayNumberBtn[17].fn = gui.bseventeen.fn
			arrayNumberBtn[17].w = gui.bseventeen.w
			arrayNumberBtn[17].h = gui.bseventeen.h
		arrayNumberBtn[18]= display.newImageRect(mainHomeGroup, gui.beighteen.fn, gui.beighteen.w, gui.beighteen.h)
			arrayNumberBtn[18].fn = gui.beighteen.fn
			arrayNumberBtn[18].w = gui.beighteen.w
			arrayNumberBtn[18].h = gui.beighteen.h
		arrayNumberBtn[19]= display.newImageRect(mainHomeGroup, gui.bnineteen.fn, gui.bnineteen.w, gui.bnineteen.h)
			arrayNumberBtn[19].fn = gui.bnineteen.fn
			arrayNumberBtn[19].w = gui.bnineteen.w
			arrayNumberBtn[19].h = gui.bnineteen.h
		for i = 0,9 do
			arrayNumber[i].alpha = 0
		end
		for i  = 0,19 do
			arrayNumberBtn[i].alpha = 0
		end
	end
	creatArrayNumber()
	function ViewBoard ()
		statusBoard = "play"
		btnPause.alpha = 1
		txtTimeScore.alpha = 1
		number1 = math.random(1,9)
		number2 = math.random(1,9)
		arg = math.random(1,2)
		if arg == 1 then
			sum.alpha = 1
			sub.alpha = 0
			result = number1+number2
		else 
			sum.alpha = 0
			sub.alpha = 1
			while number1<number2 do
				number2 = math.random(1,9)
			end
			result = number1 - number2
		end
		arrayArgument[1] = display.newImageRect(mainHomeGroup, arrayNumber[number1].fn, arrayNumber[number1].w, arrayNumber[number1].h)
		arrayArgument[1].x = gui.argument1.x
		arrayArgument[1].y = gui.argument1.y
		arrayArgument[2] = display.newImageRect(mainHomeGroup, arrayNumber[number2].fn, arrayNumber[number2].w, arrayNumber[number2].h)
		arrayArgument[2].x = gui.argument2.x
		arrayArgument[2].y = gui.argument2.y
		equal.alpha = 1
		randomPosition = math.random(1,3) -- vị trí ngẫu nhiên của kết quả
		---tạo ngẫu nhiên 2 kết quả sai
		randomResult1 = math.random(1,19)
		while randomResult1 == result do
			randomResult1 = math.random(1,19)
		end
		randomResult2 = math.random(1,19)
		while randomResult2 == result or randomResult2 == randomResult1 do
			randomResult2 = math.random(1,19)
		end
		arrayButton[randomPosition] = display.newImageRect(mainHomeGroup, arrayNumberBtn[result].fn,arrayNumberBtn[result].w, arrayNumberBtn[result].h )
		arrayButton[randomPosition].x = arrayImageButton[randomPosition].x
		arrayButton[randomPosition].y = arrayImageButton[randomPosition].y
		arrayButton[randomPosition].id = result
		arrayCount[randomPosition] = 0
		if randomPosition == 1 then
			arrayButton[2] = display.newImageRect(mainHomeGroup, arrayNumberBtn[randomResult1].fn,arrayNumberBtn[randomResult1].w, arrayNumberBtn[randomResult1].h )
			arrayButton[2].x = arrayImageButton[2].x
			arrayButton[2].y = arrayImageButton[2].y
			arrayButton[2].id = randomResult1
			arrayCount[2] = 1
			arrayButton[3] = display.newImageRect(mainHomeGroup, arrayNumberBtn[randomResult2].fn,arrayNumberBtn[randomResult2].w, arrayNumberBtn[randomResult2].h )
			arrayButton[3].x = arrayImageButton[3].x
			arrayButton[3].y = arrayImageButton[3].y
			arrayButton[3].id = randomResult2
			arrayCount[3] = 1
		end
		if randomPosition == 2 then
			arrayButton[1] = display.newImageRect(mainHomeGroup, arrayNumberBtn[randomResult1].fn,arrayNumberBtn[randomResult1].w, arrayNumberBtn[randomResult1].h )
			arrayButton[1].x = arrayImageButton[1].x
			arrayButton[1].y = arrayImageButton[1].y
			arrayButton[1].id = randomResult1
			arrayCount[1] = 1
			arrayButton[3] = display.newImageRect(mainHomeGroup, arrayNumberBtn[randomResult2].fn,arrayNumberBtn[randomResult2].w, arrayNumberBtn[randomResult2].h )
			arrayButton[3].x = arrayImageButton[3].x
			arrayButton[3].y = arrayImageButton[3].y
			arrayButton[3].id = randomResult2
			arrayCount[3] = 1
		end
		if randomPosition == 3 then
			arrayButton[1] = display.newImageRect(mainHomeGroup, arrayNumberBtn[randomResult1].fn,arrayNumberBtn[randomResult1].w, arrayNumberBtn[randomResult1].h )
			arrayButton[1].x = arrayImageButton[1].x
			arrayButton[1].y = arrayImageButton[1].y
			arrayButton[1].id = randomResult1
			arrayCount[1] = 1
			arrayButton[2] = display.newImageRect(mainHomeGroup, arrayNumberBtn[randomResult2].fn,arrayNumberBtn[randomResult2].w, arrayNumberBtn[randomResult2].h )
			arrayButton[2].x = arrayImageButton[2].x
			arrayButton[2].y = arrayImageButton[2].y
			arrayButton[2].id = randomResult2
			arrayCount[2] = 1
		end
		-- txtCount[1].text = arrayCount[1]
		-- txtCount[2].text = arrayCount[2]
		-- txtCount[3].text = arrayCount[3]
		for i = 1,3 do
			arrayButton[i]:addEventListener("touch", onTouchButton)
		end
		keyListener = true
	end
	function onReplayButtonTap()
		if keyListener == true then
			for k,v in pairs(arrayButton) do
				v:removeEventListener("touch", onTouchButton)
			end
			keyListener = false
		end
		Init()
	end
	function onTouchButton(event)
		phase = event.phase
		if (phase == "began") then
			local t = event.target
			if t.id == arrayButton[1].id  then
				arrayCount[1] = arrayCount[1] - 1
				--txtCount[1].text = arrayCount[1]
			elseif t.id == arrayButton[2].id then
				arrayCount[2] = arrayCount[2] - 1
				--txtCount[2].text = arrayCount[2]
			elseif t.id == arrayButton[3].id then
				arrayCount[3] = arrayCount[3] - 1
				--txtCount[3].text = arrayCount[3]
			end
			checkWin()
			for i =1,3 do
				if arrayCount[i] < 0 then
					gameOver.alpha = 1
					if keyListener == true then
						for k,v in pairs(arrayButton) do
							v:removeEventListener("touch",onTouchButton)
						end
						keyListener = false
					end
					timer.cancel(timer_countdown)
					timer.cancel(timer_countdownReady)
				end
			end
		end
	end
	function finishedGame()
		if timeScore > 1.1 then
			numberStar = 0
		elseif timeScore == 1.1 then
			numberStar = 1
		elseif timeScore == 1.0 then
			numberStar = 2
		elseif timeScore == 0.9 then
			numberStar = 3
		elseif timeScore <= 0.8 and timeScore > 0.6 then
			numberStar = 4
		elseif timeScore <= 0.6 then
			numberStar = 5
		end
		local g1
			g1 = require "popup.popupWin".new(timeScore*100,numberStar,arrayScore,onReplayButtonTap)
		transition.to(g1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic})
	end
	function checkWin()
		for i = 1,10 do
			arrayTimeScore[i] = 0
		end
		if arrayCount[1] == 0 and arrayCount[2] == 0 and arrayCount[3] == 0 then
			statusBoard = "play"
			countturn = countturn+1
			arrayTimeScore[countturn] = (math.round((tonumber((deltaTime-time1)))/100))/10
			print (arrayTimeScore[countturn])
			timer.cancel(timer_countdown)
			for i = 1,10 do
				timeTotalScore = timeTotalScore + arrayTimeScore[i]
				i = i+1
			end
			sum.alpha = 0
			sub.alpha = 0
			equal.alpha = 0
			timeScore = math.round((timeTotalScore/countturn)*10)/10
			txtTimeScore.text = timeScore
			txtTimePlay.alpha = 0
			for j = 1,3 do
				display.remove(arrayButton[j])
				keyListener = false
			end
			for i = 1,2 do
				display.remove(arrayArgument[i])
			end
			if countturn <  10 then
				timeReady = 1
				if arrayTimeScore[countturn] <= 0.6 then
					iconPerfect.alpha = 1
					iconGood.alpha = 0
					iconOk.alpha = 0
				elseif arrayTimeScore[countturn]>0.6 and arrayTimeScore[countturn] <=0.9 then
					iconGood.alpha = 1
					iconOk.alpha = 0
					iconPerfect.alpha = 0
				elseif arrayTimeScore[countturn]>0.9 and arrayTimeScore[countturn] <= 1.2 then
					iconOk.alpha = 1
					iconGood.alpha = 0
					iconPerfect.alpha = 0
				end
				print (countturn)
				if countturn>5 and countturn <10 then
					deltaTime = deltaTime -100
				end
				--print("deltaTime = "..deltaTime)
				txtTimeReady.alpha = 0
				statusBoard = "wait"
				timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
			else
				 timefinish = timer.performWithDelay(300,function()
																	finishedGame()
																end ,1)
			end
		end
	end
	function countdownTime (event)
		txtTimePlay.alpha = 1
		time1 = time1 - 100
		 txtTimePlay.text = (math.round((time1)/100))/10 ..""
		 if( time1 <0) then
			  txtTimePlay.text = "0"
			  timer.cancel(timer_countdown)
			  mainHomeGroup:insert(gameOver)
			  gameOver.alpha = 1
			  if keyListener == true then
				  for i = 1,3 do
					arrayButton[i]:removeEventListener("touch", onTouchButton)
				  end
				  keyListener = false
			  end
		end
	end
	onStartPopupPause = function()
		if(timer_countdown ~= nil)then
			timer.pause(timer_countdown)
		end
		if (timer_countdownReady ~= nil ) then
			timer.pause(timer_countdownReady)
		end
		if keyListener == true then
			for i = 1,3 do
				if arrayButton[i] ~= nil then
					arrayButton[i]:removeEventListener("touch", onTouchButton)
				end
			end
			keyListener = false
		end
	end
	onResumePopup = function()
		if(timer_countdown ~= nil)then
			timer.resume(timer_countdown)
		end
		if(timer_countdownReady ~= nil)then
			timer.resume(timer_countdownReady)
		end
		if keyListener == false and statusBoard == "play" then
			for i = 1,3 do
				if arrayButton[i] ~= nil then
					arrayButton[i]:addEventListener("touch", onTouchButton)
				end
			end
			keyListener = true
		end
	end
	function Pause()
		local g1 = require "popup.popupPause".new(onResumePopup,onReplayButtonTap)
			local group1 = display.newGroup()
			group1:insert(g1)
			mainHomeGroup:insert(group1)
			group1.y = -300
			transition.to(group1,{time = 200,alpha = 1, x = 0, y = 0, transition = wEasing.easeOutElastic, onStart = onStartPopupPause})
		
	end
	
	function countdownReady ()
		timeReady = timeReady - 1
			txtTimeReady.text = timeReady
		if timeReady == 0 then
			iconOk.alpha = 0
			iconGood.alpha = 0
			iconPerfect.alpha = 0
			txtTimeReady.alpha = 0
			go.alpha = 1
			--txtTimeReady.text = "GO"
		end
		if timeReady <0 then
			txtTimeReady.alpha = 0
			go.alpha = 0
			timer.cancel(timer_countdownReady)
			ViewBoard()
			txtTimePlay.alpha = 1
			time1 = deltaTime
			timer_countdown = timer.performWithDelay(delay, countdownTime, loop)
			timeSystem = system.getTimer()
		end
	end
	timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
	function Init()
		timeReady = 1
		txtTimeScore.alpha = 0
		txtTimeReady.text = "3"
		statusBoard = "start"
		txtTimeReady.alpha = 1
		txtTimePlay.alpha = 0
		deltaTime = 1600
		countturn = 0
		equal.alpha = 0
		sum.alpha = 0
		sub.alpha =0
		iconOk.alpha = 0
		iconGood.alpha = 0
		iconPerfect.alpha = 0
		gameOver.alpha = 0
		timeScore = 0
		timeTotalScore = 0
		txtTimeScore.text = timeScore
		for i = 1,3 do
			arrayCount[i] = 0
			--txtCount[i].text = arrayCount[i]
		end
		if timer_countdown ~= nil then
			timer.cancel(timer_countdown)
		end
		if timer_countdownReady ~= nil then
			timer.cancel(timer_countdownReady)
		end
		for j = 1,3 do
			if arrayButton[j] ~= nil then
				display.remove(arrayButton[j])
			end
		end
		for i = 1,2 do
			display.remove(arrayArgument[i])
		end
		timer_countdownReady = timer.performWithDelay(1000,countdownReady,0)
		
	end
		
	bg = display.newImageRect(mainHomeGroup, gui.bg.fn, gui.bg.w, gui.bg.h)
		bg.x = gui.bg.x
		bg.y = gui.bg.y
	for i = 1,3 do
		arrayImageButton[i] = display.newImageRect(mainHomeGroup, gui.btnButton.fn, gui.btnButton.w, gui.btnButton.h)
		arrayImageButton[i].x = gui.btnButton.x + a
		arrayImageButton[i].y = gui.btnButton.y
		a = a+100
	end
	go = display.newImageRect(mainHomeGroup, gui.go.fn, gui.go.w, gui.go.h)
		go.x = gui.go.x
		go.y = gui.go.y
		go.alpha = 0
	-- txtCount[1] = display.newText(mainHomeGroup,arrayCount[1], 95, 380, native.systemFont,20)
		-- txtCount[1]:setTextColor(0,0,0)
	-- txtCount[2] = display.newText(mainHomeGroup,arrayCount[2],195,380,native.systemFont,20)
		-- txtCount[2]:setTextColor(0,0,0)
	-- txtCount[3] = display.newText(mainHomeGroup,arrayCount[3],295,380,native.systemFont,20)
		-- txtCount[3]:setTextColor(0,0,0)
	txtTimeScore = display.newText(mainHomeGroup,timeScore, gui.txtTimeScore.x, gui.txtTimeScore.y, gui.txtTimeScore.font, gui.txtTimeScore.size)
		txtTimeScore:setTextColor(unpack(gui.txtTimeScore.color))
		txtTimeScore.alpha = 0
	txtTimeReady = display.newText(mainHomeGroup, timeReady,gui.txtTimeReady.x -20, gui.txtTimeReady.y, gui.txtTimeReady.font, gui.txtTimeReady.size)
		txtTimeReady:setTextColor(unpack(gui.txtTimeReady.color))
		txtTimeReady.alpha = 0
	txtTimePlay = display.newText(mainHomeGroup, deltaTime/1000,gui.txtTimePlay.x -30, gui.txtTimePlay.y, gui.txtTimePlay.font, gui.txtTimeReady.size)
		txtTimePlay:setTextColor(unpack(gui.txtTimePlay.color))
		txtTimePlay.alpha = 0
	iconOk = display.newImageRect(mainHomeGroup, gui.iconOk.fn, gui.iconOk.w, gui.iconOk.h)
		iconOk.x = gui.iconOk.x
		iconOk.y = gui.iconOk.y
		iconOk.alpha= 0
	iconGood = display.newImageRect(mainHomeGroup, gui.iconGood.fn, gui.iconGood.w, gui.iconGood.h)
		iconGood.x = gui.iconGood.x
		iconGood.y = gui.iconGood.y
		iconGood.alpha= 0
	iconPerfect = display.newImageRect(mainHomeGroup, gui.iconPerfect.fn, gui.iconPerfect.w, gui.iconPerfect.h)
		iconPerfect.x = gui.iconPerfect.x
		iconPerfect.y = gui.iconPerfect.y
		iconPerfect.alpha= 0
	sum = display.newImageRect(mainHomeGroup, gui.sum.fn, gui.sum.w, gui.sum.h)
		sum.x = gui.sum.x
		sum.y = gui.sum.y
		sum.alpha = 0
	sub = display.newImageRect(mainHomeGroup, gui.sub.fn, gui.sub.w, gui.sub.h)
		sub.x = gui.sub.x
		sub.y = gui.sub.y
		sub.alpha= 0
	equal = display.newImageRect(mainHomeGroup, gui.equal.fn, gui.equal.w, gui.equal.h)
		equal.x = gui.equal.x
		equal.y = gui.equal.y
		equal.alpha = 0
	gameOver = display.newText(mainHomeGroup,"GAME OVER", gui.txtTimeReady.x, gui.txtTimeReady.y+30, gui.txtTimeReady.font, gui.txtTimeReady.size)
		gameOver.x = display.contentCenterX
		gameOver:setTextColor(255,0,0)
		gameOver.alpha = 0
	win = display.newText(mainHomeGroup,"YOU WIN", gui.txtTimeReady.x, 150, gui.txtTimeReady.font, gui.txtTimeReady.size+20)
		win.x = display.contentCenterX
		win.y = display.contentCenterY
		win:setTextColor(unpack(gui.txtTimeReady.color))
		win.alpha = 0
	btnPause = ui.newButton({ defaultSrc = gui.btnPause.fn,
											overSrc = gui.btnPause.fn,
											defaultX = gui.btnPause.w,
											defaultY = gui.btnPause.h,
											overX = gui.btnPause.w,
											overY = gui.btnPause.h,
											overAlpha = gui.btnPause.overAlpha,
											overScale = gui.btnPause.overScale,
											x = gui.btnPause.x,
											y = gui.btnPause.y,
											onRelease = Pause
										})
		mainHomeGroup:insert(btnPause)
		btnPause.alpha = 1
	return mainHomeGroup
	
	-----------------------------------------------------------------------------
	
end


-- Called when scene is about to move offscreen:
function scene:exitScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	
	-----------------------------------------------------------------------------
	
end


-- Called prior to the removal of scene's "view" (display group)
function scene:destroyScene( event )
	local group = self.view
	
	-----------------------------------------------------------------------------
	
	--	INSERT code here (e.g. remove listeners, widgets, save state, etc.)
	
	-----------------------------------------------------------------------------
	
end

---------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------

-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )

-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )

-- "exitScene" event is dispatched before next scene's transition begins
scene:addEventListener( "exitScene", scene )

-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )

---------------------------------------------------------------------------------

return scene
